﻿using Gengens.Disk.Entity.model.file;
using Gengens.Disk.Service.service;
using Gengens.Disk.WebApi.dto.input.file;
using Gengens.Disk.WebApi.dto.output;
using Microsoft.AspNetCore.Mvc;

namespace Gengens.Disk.WebApi.Controllers.file
{
    /// <summary>
    /// 目录接口
    /// </summary>
    public class FilePathController : BaseUserController
    {
        private readonly FilePathInfoService _filePathInfoService;

        public FilePathController(FilePathInfoService filePathInfoService)
        {
            _filePathInfoService = filePathInfoService;
        }

        #region 查询

        [HttpGet]
        public async Task<ApiRet> All()
        {
            var ret = await _filePathInfoService.SelectAsync(f => f.UserId == UserId);
            return ApiRet.Ok(string.Empty, ret);
        }

        #endregion

        #region 新增

        [HttpPost]
        public async Task<ApiRet> Add([FromBody]FilePathInfoForCreateDto createDto) 
        {
            FilePathInfo? basePath = default;
            if (createDto.BasePathId.HasValue) {
                basePath = _filePathInfoService.Find(createDto.BasePathId.Value);
                if (basePath == default) {
                    return ApiRet.Fail($"找不到id为{createDto.BasePathId}的目录");
                }
            }

            FilePathInfo path = new FilePathInfo()
            { 
                Name = createDto.Name,
                BasePathId = createDto.BasePathId,
                BasePathName = basePath?.Name,
                UserId = UserId,
            };

            var newPath = await _filePathInfoService.InsertAsync(path);
            await _filePathInfoService.SaveChanegsAsync();

            return ApiRet.Ok("新增成功", newPath);
        }

        #endregion

        #region 删除

        [HttpDelete("{id}")]
        public async Task<ApiRet> Delete([FromQuery]Guid id) {
            var path = _filePathInfoService.Find(id);
            if(path == default)
            {
                return ApiRet.Fail($"找不到id为{id}的目录");
            }

            _filePathInfoService.Delete(path);


        }

        #endregion



    }
}
